package com.evil.common.file.enums;

import com.evil.common.core.enums.BaseEnum;
import com.evil.common.core.exception.BusinessException;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author liyang
 */
@AllArgsConstructor
public enum BusTypeEnum {

    /**
     * 文件业务类型
     */
    OTHER(0, "其他", false, null),
    PORTRAIT(1, "头像", false, null),
    APPLICATION_ORDER_FOLDER_FILE(2, "应用订单文件夹文件", true, null),
    APPLICATION_ORDER_TASK_FILE(3, "应用订单任务附件", true, null),
    APPLICATION_FORM_FILE(4, "应用表单附件", true, null),
    APPLICATION_ORDER_CONTROL_FILE(5, "应用订单控件附件", true, null),
    APPLICATION_ORDER_TINYMCE_CONTROL_FILE(6, "应用表单控件附件（富文本）", false, null),
    APPLICATION_ORDER_MESSAGE_FILE(7, "应用订单留言附件", false, null),
    APPLICATION_PRINT_TEMPLATE_FILE(8, "应用打印模板文件", true, null),
    APPLICATION_ORDER_IMPORT_FILE(9, "应用订单导入文件", false, null),
    APPLICATION_ORDER_IMPORT_ERROR_FILE(11, "应用订单导入失败文件", false, null),
    APPLICATION_EXPORT_FILE(12, "应用订单导出文件", false, null),
    APPLICATION_PRINT_FILE(13, "应用打印文件", false, null),
    ;

    @Getter
    private final int id;
    @Getter
    private final String name;
    @Getter
    private final boolean isPrivate;
    @Getter
    private final String[] permissionCode;

    public static Optional<BusTypeEnum> isExistById(Integer id) {
        if (null == id) {
            return Optional.empty();
        }
        return Arrays.stream(values()).filter(e -> e.id == id).findFirst();
    }

    public static BusTypeEnum findById(Integer id) {
        return BusTypeEnum.isExistById(id).orElseThrow(BusTypeEnum::getBusinessException);
    }

    public static List<Integer> getIds() {
        return Arrays.stream(values()).map(BusTypeEnum::getId).collect(Collectors.toList());
    }

    public static BusinessException getBusinessException() {
        return new BusinessException(BaseEnum.ERROR_PARAMETER, "文件业务类型参数错误");
    }
}
